<template>
	<view>
		<view
			:class="playOrNot?'play-bar play-bar_play':'play-bar'"
			:style="Css"
			@click.stop="clickPlay">
			<view class="play-animation">
				<view :class="playOrNot?'carrier playcenter':'carrier'">
					<view class="public T1"></view>
					<view class="public T2"></view>
					<view class="public T3"></view>
				</view>
			</view>
			<view class="duration" v-if="duration">{{duration}}"</view>
			<view class="text">播放</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"public-audio",
		data() {
			return {
				playOrNot:false,
				duration:"",
				Y:false
			};
		},
		props: {
			Url: {
				type: String,
				default: ""
			},
			Css: {
				type: String,
				default: ""
			}
		},
		created() {
			this.Y = uni.createInnerAudioContext();
		},
		methods: {
			clickPlay(){
				this.Y.src = this.Url;
				if(this.playOrNot){
					this.Y.pause();
					this.playOrNot = !this.playOrNot;
				}else{
					this.Y.play();
					this.playOrNot = !this.playOrNot;
				}
				// 自然播放结束
				this.Y.onEnded(() => {
					this.playOrNot = false;
				})
				// 音频暂停事件
				this.Y.onPause(() => {
					this.playOrNot = false;
				});
				// 音频进入可以播放状态，但不保证后面可以流畅播放
				this.Y.onCanplay(() => {
					this.duration = this.Y.duration;
				})
			}
		},
		// 销毁
		destroyed(){
			this.Y.destroy();
		}
	}
</script>

<style lang="scss">
.play-bar{
	width: 300rpx;
	height: 58rpx;
	display: flex;
	align-items: center;
	background: $FuZhuSe;
	border-radius: 10rpx;
	.play-animation {
		margin-left: 30rpx;
		.carrier {
			width: 50rpx;
			height: 50rpx;
			box-sizing: border-box;
			overflow: hidden;
			transform: rotate(135deg) translate3d(0, 0, 0);
			-webkit-transform: rotate(135deg) translate3d(0, 0, 0);
			backface-visibility: hidden;
			-webkit-backface-visibility: hidden;
		}
			
		.public {
			border: 5rpx solid #fff;
			border-radius: 50%;
			position: absolute;
		}
			
		.T1 {
			width: 5rpx;
			height: 5rpx;
			background: #fff;
			top: 45rpx;
			left: 45rpx;
		}
			
		.T2 {
			width: 25rpx;
			height: 25rpx;
			top: 35rpx;
			left: 35rpx;
		}
			
		.T3 {
			width: 40rpx;
			height: 40rpx;
			top: 25rpx;
			left: 25rpx;
		}
			
		.playcenter {
			.T2 {
				animation: fadeInOut 1s infinite 0.2s;
				-webkit-animation: fadeInOut 1s infinite 0.2s;
			}
			
			.T3 {
				animation: fadeInOut 1s infinite 0.4s;
				-webkit-animation: fadeInOut 1s infinite 0.4s;
			}
		}
			
		@keyframes fadeInOut {
			0% {
				opacity: 0;
				/*初始状态 透明度为0*/
			}
			
			100% {
				opacity: 1;
				/*结尾状态 透明度为1*/
			}
		}
	}
	.duration{
		height: 58rpx;
		display: flex;
		align-items: center;
		font-size: 22rpx;
		line-height: 22rpx;
		color: #FFF;
		margin-left: -20rpx;
	}
	.text{
		height: 58rpx;
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		margin-right: 20rpx;
		font-size: 22rpx;
		line-height: 22rpx;
		color: #FFF;
	}
}
.play-bar_play{
	background: $LvSe;
}
</style>